package com.tledu.spring.core.exception;

import com.tledu.spring.core.constants.CommonConstants;
import com.tledu.spring.core.entity.AjaxResult;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.security.auth.login.LoginException;

/**
 * @Author: rupert-tears
 * @Date: Created in 22:24 2021/7/2
 * @Description: 优秀的人都在孤独的翻山越岭！
 *
 * 控制层全局异常
 */
@ControllerAdvice
public class ExceptionControllerAdvice {
    /**
     * 登录异常
     * @param e 异常信息
     * @return 相应前台结果集
     */
    @ExceptionHandler(value = {LoginException.class})
    @ResponseBody
    public AjaxResult<String> handlerException(Exception e){
        return new AjaxResult<>(CommonConstants.AJAX_RESULT_MSG_FAIL_CODE,e.getMessage());
    }

    /**
     * 系统错误页面跳转
     * @param e 异常
     * @param model 错误信息
     * @return 错误页面
     */
    @ExceptionHandler(value = {Exception.class})
    public String error(Exception e, Model model){
        model.addAttribute("error", e);
        return "error";
    }
}

